package com.kmust.dream.wanderingdream.shoppingcart.controller;

import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.shoppingcart.entity.ShoppingCartItem;
import com.kmust.dream.wanderingdream.shoppingcart.feign.ProductSkuFeign;
import com.kmust.dream.wanderingdream.shoppingcart.service.ShoppingCartItemService;
import com.kmust.dream.wanderingdream.shoppingcart.utils.ReturnUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop/shoppingCart")
@Api("收藏夹内容API")
public class ShoppingCartItemController {

    @Autowired
    private ShoppingCartItemService ShoppingCartItemService;
    @Autowired
    private ProductSkuFeign productSkuFeign;

    /**
     * 获取所有的购物车内容
     *
     * @return ResultBean
     */
    @ApiOperation("获取所有的购物车内容，不用")
    @GetMapping("/getAllItems")
    public ResultBean getAllItems() {
        return ShoppingCartItemService.getAllItems();
    }

    /**
     * 通过用户名获取所有的购物车内容
     *
     * @param username username
     * @return resultBean
     */
    @ApiOperation("通过用户名获取所有的购物车内容")
    @GetMapping("/getItemsByUsername/{username}")
    public ResultBean getItemsByUsername(@PathVariable String username) {
        ResultBean res = ShoppingCartItemService.getItemsByUsername(username);
        if (String.valueOf(res.getStatus()).startsWith("2")) {
            List<ShoppingCartItem> list = (List<ShoppingCartItem>) res.getPayload();
            for (ShoppingCartItem shoppingCartItem : list) {
                ResultBean version = this.getVersion(shoppingCartItem.getItemId());
                if (String.valueOf(version.getStatus()).startsWith("2")) {
                    Integer oldVersion = (Integer) version.getPayload();
                    shoppingCartItem.setVersion(oldVersion);
                } else {
                    return ReturnUtils.getOne(null);
                }
            }
        }
        return ReturnUtils.getOne(null);
    }


    /**
     * 添加到购物车
     *
     * @param shoppingCartItem 购物车item json
     * @return resultBean
     */
    @ApiOperation("添加购物车")
    @PostMapping("/addItem")
    public ResultBean addItem(@RequestBody ShoppingCartItem shoppingCartItem) {
        return ShoppingCartItemService.addItem(shoppingCartItem);
    }

    /**
     * 修改购物车内容
     *
     * @param shoppingCartItem 购物车item json
     * @return resultBean
     */
    @ApiOperation("修改购物车内容")
    @PutMapping("/updateItem")
    public ResultBean updateItem(@RequestBody ShoppingCartItem shoppingCartItem) {
        return ShoppingCartItemService.updateItem(shoppingCartItem);
    }

    /**
     * 假删除购物车内容
     *
     * @param itemId 主键
     * @return resultBean
     */
    @ApiOperation("假删除购物车内容")
    @DeleteMapping("/deleteItem/{itemId}")
    public ResultBean deleteItem(@PathVariable Integer itemId) {
        return ShoppingCartItemService.deleteItem(itemId);
    }


    /**
     * 获取sku的version
     *
     * @param itemId 主键
     * @return resultBean
     */
    @ApiOperation("获取version")
    @GetMapping("/getVersion/{itemId}")
    public ResultBean getVersion(@PathVariable Integer itemId) {
        ShoppingCartItem byId = ShoppingCartItemService.getById(itemId);
        Integer productSkuId = byId.getProductSkuId();
        ResultBean skuById = productSkuFeign.getSkuVersion(productSkuId);
        if (String.valueOf(skuById.getStatus()).startsWith("2")) {
            Integer version = (Integer) skuById.getPayload();
            return ReturnUtils.getOne(version);
        }
        return ReturnUtils.getOne(null);
    }

    @GetMapping("/getSciDel/{username}")
    public ResultBean getSciDel(@PathVariable String username) {
        return ShoppingCartItemService.getSciDel(username);
    }
}
